﻿using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace YGBookmark.Models
{
    /// <summary>
    /// 书签标签关联实体（多对多关系中间表）
    /// </summary>
    [Table("BookmarkTags")]
    public class BookmarkTag
    {
        /// <summary>
        /// 书签ID（复合主键）
        /// </summary>
        [Required]
        [Column(Order = 0)]
        public long BookmarkId { get; set; }

        /// <summary>
        /// 标签ID（复合主键）
        /// </summary>
        [Required]
        [Column(Order = 1)]
        public long TagId { get; set; }

        /// <summary>
        /// 关联书签导航属性
        /// </summary>
        [ForeignKey(nameof(BookmarkId))]
        public virtual Bookmark Bookmark { get; set; }

        /// <summary>
        /// 关联标签导航属性
        /// </summary>
        [ForeignKey(nameof(TagId))]
        public virtual Tag Tag { get; set; }

        /// <summary>
        /// 无参构造函数（ORM需要）
        /// </summary>
        public BookmarkTag() { }

        /// <summary>
        /// 创建关联关系的构造函数
        /// </summary>
        public BookmarkTag(int bookmarkId, int tagId)
        {
            BookmarkId = bookmarkId;
            TagId = tagId;
        }

        /// <summary>
        /// 重写Equals方法（复合主键比较）
        /// </summary>
        public override bool Equals(object obj) =>
            obj is BookmarkTag other &&
            BookmarkId == other.BookmarkId &&
            TagId == other.TagId;

        /// <summary>
        /// 重写GetHashCode方法（复合主键哈希）
        /// </summary>
        public override int GetHashCode() =>
            HashCode.Combine(BookmarkId, TagId);

        /// <summary>
        /// 输出友好格式
        /// </summary>
        public override string ToString() =>
            $"{BookmarkId}:{TagId}";
    }
}